﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ddb_BT1
{
    public partial class frmQuanTriNguoiDung : DevExpress.XtraEditors.XtraForm
    {
        public frmQuanTriNguoiDung()
        {
            InitializeComponent();
            treeView1.ExpandAll();
        }
        List<string> CHUCNANG= new List<string>();
        private void frmQuanTriNguoiDung_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = DataProvider.getDataTable("select username from nguoidung where quyenhan>1");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                listBoxControl1.Items.Add(dt.Rows[i][0].ToString().ToUpper());
            }
        }

        private void listBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CHUCNANG.Clear();
            DataTable dt = DataProvider.getDataTable(string.Format("select * from QUANTRI where username='{0}'", listBoxControl1.SelectedItem));
            CHUCNANG.AddRange(dt.Rows[0][1].ToString().Split('-'));
            CallRecursive(treeView1);
        }

        private void PrintRecursive(TreeNode treeNode)
        {

            if (treeNode.Parent!=null) treeNode.Checked = treeNode.Parent.Checked;
            // Print the node.
        //    System.Diagnostics.Debug.WriteLine(treeNode.Text);
          //  XtraMessageBox.Show(treeNode.Text);
            if (CHUCNANG.IndexOf(treeNode.Text)>-1)
            {
                treeNode.Checked = false;
            }
           
         //   MessageBox.Show(treeNode.Text);
            // Print each node recursively.
            foreach (TreeNode tn in treeNode.Nodes)
            {
                PrintRecursive(tn);
            }
        }

        // Call the procedure using the TreeView.
        private void CallRecursive(TreeView treeView)
        {
            // Print each node recursively.
            TreeNodeCollection nodes = treeView.Nodes;
            foreach (TreeNode n in nodes)
            {
                n.Checked = true;
                PrintRecursive(n);
            }
        }
        string save = "";
        private void btnSave_Click(object sender, EventArgs e)
        {
            save = "";
            CallRecursive_duyet(treeView1);
            save.ToUpper();
            DataProvider.AffectData(string.Format("update quantri set chucnang=N'{0}' where username=N'{1}'",save,listBoxControl1.SelectedItem));

        }

        private void PrintRecursive_duyet(TreeNode treeNode)
        {

            if (treeNode.Checked == false)
            {
                save = save + treeNode.Text+"-";
            }
            foreach (TreeNode tn in treeNode.Nodes)
            {
                PrintRecursive_duyet(tn);
            }
        }

        // Call the procedure using the TreeView.
        private void CallRecursive_duyet(TreeView treeView)
        {
          
            TreeNodeCollection nodes = treeView.Nodes;
            foreach (TreeNode n in nodes)
            {
                PrintRecursive_duyet(n);
            }
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            // The code only executes if the user caused the checked state to change. 
            if (e.Action != TreeViewAction.Unknown)
            {
                if (e.Node.Nodes.Count > 0)
                {
                    /* Calls the CheckAllChildNodes method, passing in the current 
                    Checked value of the TreeNode whose checked state changed. */
                    this.CheckAllChildNodes(e.Node, e.Node.Checked);
                }
            }
        }
        // Updates all child tree nodes recursively. 
        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    // If the current node has child nodes, call the CheckAllChildsNodes method recursively. 
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }

        private void PrintRecursive_duye_con(TreeNode treeNode,bool check)
        {

          // treeNode.Checked == treeNode.Parent.Checked;
           
            foreach (TreeNode tn in treeNode.Nodes)
            {
                PrintRecursive_duye_con(tn,check);
            }
        }
     
    }
}